package myModule;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.DriverManager;
import java.sql.ResultSet;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.websocket.SendResult;

import org.apache.catalina.mbeans.ClassNameMBean;

import sun.org.mozilla.javascript.internal.json.JsonParser;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.Statement;

/**
 * Servlet implementation class LoginForm
 */
public class LoginForm extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private Connection conn;
    private PreparedStatement stmt;
    private String adminName = "" , adminPass = "" , driver = "" , connector = "";   
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginForm() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		// get Admin Data
		adminName = request.getParameter("adminName");
		adminPass = request.getParameter("adminPass");
		
		// get config info for database connection
		driver = "com.mysql.jdbc.Driver";
		connector = "jdbc:mysql://localhost/ibm_training";
		// jdbc process
		try{
			Class.forName(driver);
			conn = (Connection)DriverManager.getConnection(connector,"root","");
			
			stmt = (PreparedStatement)conn.prepareStatement("SELECT * FROM admin WHERE name = ? AND password = ?");
			stmt.setString(1,adminName);
			stmt.setString(2,adminPass);
			
			ResultSet rs = stmt.executeQuery();
			PrintWriter out = response.getWriter();
			 while(rs.next()){
		           //Retrieve by column name
		           int id  = rs.getInt("id");
		           String name = rs.getString("name");
		           String password = rs.getString("password");
		           String email = rs.getString("email");
		           
		           //Display values
		           out.println("ID: " + id + "<br>");
		           out.println("name: " + name + "<br>");
		           out.println("password: " + password + "<br>");
		           out.println("email: " + email + "<br>");
		        }
		}catch(Exception e){
			e.printStackTrace();
		}
	}
}
